<template>
	<view>
		<view class="search-top">
			<uni-search-bar radius="100" placeholder="搜索" @confirm="search" />
		</view>
		<scroll-view class="scroll-list" scroll-y="true" @scrolltolower="fnLower">
			<view class="house-box" v-for="item in swipeList" :key="item" @click="fnTurnDetail(item)">
				<image src="../../static/demo.jpg" mode="aspectFill"></image>
				<view>
					<view>{{item.title}}·{{item.house_layout}}</view>
					<view>
						<text>{{item.address}}</text>
						<text>买<text>{{item.saler_ratio}}</text>卖<text>{{item.seller_ratio}}</text></text>
						<text class="c-main">{{item.house_price}}万</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import store from '@/store/store.vue'
import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
export default {
	onShow(){
		this.getHouseListByUser()
	},
	components: {
		uniSearchBar
	},
	data() {
		return {
			searchVal: '',
			showClearIcon: false,
			swipeList:[]
		};
	},
	methods: {
		getHouseListByUser(){
			let _this = this				
			uni.request({
			    url: store.url + '/house/list?&house_source_type=2',
				method: 'GET',			    
			    header: {
					"content-type": "application/json;charset=utf-8"						
			    },
			    success: (res) => {		
					let _this = this
					console.log(res)
					if(res.data.status == 'success'){
						_this.swipeList = []
						let array = res.data.message
						let swipeList = []
						for(let i = 0; i < array.length; i++){		
							let arr = array[i].house_layout.split(';')
						    array[i].house_layout = arr[0] + '室' + arr[1] + '厅' + arr[2] + '卫'
							arr = array[i].commission_share_ratio.split(';')
							array[i].saler_ratio = arr[0]
							array[i].seller_ratio = arr[1]
							let newObj = { 
								title: array[i].community_name,
								house_layout: array[i].house_layout,
								address: array[i].address,
								commission_share_ratio: array[i].commission_share_ratio,								
								saler_ratio: array[i].saler_ratio,
								seller_ratio: array[i].seller_ratio,
								house_price: ('house_price' in array[i]) ? array[i].house_price : '',
								house_id: array[i].house_id,
								user_id: array[i].user_id,
								house_source_id: array[i].house_source_id,
								inviter_id: getApp().globalData.inviter_id,
								publisher_id: getApp().globalData.user_id
							}
							swipeList.push(newObj)
						}
						_this.swipeList = swipeList
					}										        
			    },
				fail: (res)=>{
					uni.showModal({
					    title: '提示',
					    content: '访问服务器失败: ' + res.data.message
					});						
				}
			});	
		},
		search(res) {
			uni.showToast({
				title: '搜索：' + res.value,
				icon: 'none'
			})
			let _this = this
			uni.request({
			    url: store.url + '/house/list/get_house_list_by_condition?&house_source_type=2&param=' + res.value,
				method: 'GET',			    
			    header: {
					"content-type": "application/json;charset=utf-8"						
			    },
			    success: (res) => {		
					let _this = this
					console.log(res)
					if(res.data.status == 'success'){
						_this.swipeList = []
						let array = res.data.message
						let swipeList = []
						for(let i = 0; i < array.length; i++){		
							let arr = array[i].house_layout.split(';')
						    array[i].house_layout = arr[0] + '室' + arr[1] + '厅' + arr[2] + '卫'
							arr = array[i].commission_share_ratio.split(';')
							array[i].saler_ratio = arr[0]
							array[i].seller_ratio = arr[1]
							let newObj = { 
								title: array[i].community_name,
								house_layout: array[i].house_layout,
								address: array[i].address,
								commission_share_ratio: array[i].commission_share_ratio,								
								saler_ratio: array[i].saler_ratio,
								seller_ratio: array[i].seller_ratio,
								house_price: ('house_price' in array[i]) ? array[i].house_price : '',
								house_id: array[i].house_id,
								user_id: array[i].user_id,
								house_source_id: array[i].house_source_id,
								inviter_id: getApp().globalData.inviter_id,
								publisher_id: getApp().globalData.user_id
							}
							swipeList.push(newObj)
						}
						_this.swipeList = swipeList
					}										        
			    },
				fail: (res)=>{
					uni.showModal({
					    title: '提示',
					    content: '访问服务器失败: ' + res.data.message
					});						
				}
			});
		},
		fnLower: function(e) {
			console.log(e)
		},
		fnTurnDetail(item){
			console.log(item)
			uni.navigateTo({
				url: '/pages/secondHouse/detail?id=' + item.house_id + '&user_id=' + item.user_id + '&house_source_id=' + item.house_source_id + '&publisher_id=' + item.publisher_id + '&inviter_id=' + item.inviter_id + '&title=' + item.title + '&address=' + item.address + '&house_layout=' + item.house_layout, 
			})
		}
	}
};
</script>

<style lang="scss" scoped>
	.search-top {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 9;
		
		/deep/ .uni-searchbar {
			padding: 16rpx 26rpx;
			background-color: #f13b32;
		}
		
		/deep/ .uni-searchbar__cancel {
			color: #FFF;
		}
	}
	
	.scroll-list {
		position: absolute;
		padding: 2vw 5vw 5vw;
		box-sizing: border-box;
		top: calc(32rpx + 36px);
		bottom: 0;
		left: 0;
		right: 0;
	}
	
	.house-box {
		display: flex;
		margin: 5vw 0;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		background: #FFF;
		
		image {
			margin-right: 5vw;
			width: 230rpx;
			height: 170rpx;
		}
		
		>view {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			flex: 1;
			overflow: hidden;
			
			view {
				overflow: hidden;
				text-overflow:ellipsis;
				white-space: nowrap;
				line-height: 1.6;
				font-size: 28rpx;
				
				>text {
					display: block;
					overflow: hidden;
					text-overflow:ellipsis;
					white-space: nowrap;
					font-size: 22rpx;
					
					text {
						margin: 0 4rpx;
						color: #f13b32;
					}
					
					&.c-main {
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>
